#################################################################################
#
# Copyright (c) 2006 Michigan State University
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#################################################################################

#################################################################################
#
#	Author:
#		Nathan Collins <npcollins@gmail.com>
#
#################################################################################

"""
Identify document type and return a string with <img ...> tag to 
display document icon if available. Otherwise, return empty string.
"""

import re

def detecttype(format, typelist):
	"""
	Given a list of re expresssions, check to see if any are present in
	the format string. If so, return 1. Otherwise return 0.
	"""
	# join types
	typestring = '|'.join(typelist)

	# search for expressions
	found = re.search(typestring, format, re.IGNORECASE)

	# check to see if it was found
	if found is not None:
		# something matched
		return 1

	# nothing matched
	return 0
		

def gettypeicon(format):
	"""
	Given a format string, parse for filetype. Return img tag for detected
	type if available.
	"""
	# icon types
	icons = {}
	icons['archive'] = 'docicons/file-archive.png'
	icons['html'] = 'docicons/file-html.png'
	icons['image'] = 'docicons/file-image.png'
	icons['office'] = 'docicons/file-office.png'
	icons['pdf'] = 'docicons/file-pdf.png'
	icons['ps'] = 'docicons/file-postscript.png'
	icons['sound'] = 'docicons/file-sound.png'
	icons['text'] = 'docicons/file-text.png'
	icons['video'] = 'docicons/file-video.png'

	# template for img
	template = ' <img src="%s" title="%s" width=16 height=16>'

	# detected type
	detected = ''

	### Detection
	
	# detect pdf
	dpdf = ['pdf', 'application/pdf']
	if detecttype(format, dpdf):
		return template % (icons['pdf'], 'PDF document')

	# detect jpg, jpeg, png, bmp, tif
	dimage = ['png', 'jpe?g', 'tiff?', 'bmp']
	if detecttype(format, dimage):
		return template % (icons['image'], 'Image file')

	# detect wav, mp3, aac, ogg
	dsound = ['wav', 'mp3', 'aac', 'ogg', 'midi']
	if detecttype(format, dsound):
		return template % (icons['sound'], 'Audio file')

	# detect mov, avi, wmv, mpg, mpeg
	dvideo = ['mov', 'avi', 'wmv', 'mpe?g']
	if detecttype(format, dvideo):
		return template % (icons['video'], 'Video file')
	
	# detect zip, tar, tar.gz, tar.bz, rar
	darchive = ['zip', 'tar(\.[gb]z)?', 'rar']
	if detecttype(format, darchive):
		return template % (icons['archive'], 'Archived files')

	# detect doc, odt, xls
	doffice = ['doc', 'odt', 'xls', 'ppt', 'ods', 'odp']
	if detecttype(format, doffice):
		return template % (icons['office'], 'Office document')

	# detect html, htm
	dhtml = ['html?']
	if detecttype(format, dhtml):
		return template % (icons['html'], 'Web page')

	# detect ps
	dps = [ '\.?ps', 'application/postscript', 'postscript']
	if detecttype(format, dps):
		return template % (icons['ps'], 'Postscript file')

	# detect txt
	dtext = ['txt', 'text']
	if detecttype(format, dtext):
		return template % (icons['text'], 'Text file')


	# nothing found, return empty string
	return ''

# eof

